package cn.iutils.cms.controller;

import cn.iutils.cms.entity.*;
import cn.iutils.cms.entity.enums.ModuleEnum;
import cn.iutils.cms.service.*;
import cn.iutils.common.Page;
import cn.iutils.common.controller.BaseController;
import cn.iutils.common.utils.JStringUtils;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import java.net.URLDecoder;
import java.util.List;

/**
 * 网站访问接口
 *
 * @author iutils.cn
 */
@Controller
@RequestMapping(value = "${frontPath}")
public class FrontController extends BaseController {

    @Autowired
    private SiteService siteService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private ArticleService articleService;

    @Autowired
    private ArticleDataService articleDataService;

    @Autowired
    private AlbumService albumService;

    @Autowired
    private LinkService linkService;

    @Autowired
    private CommodityService commodityService;

    /**
     * 访问主页
     *
     * @param siteId 站点ID
     * @param model
     * @return
     */
    @RequestMapping(value = "/{siteId}${urlSuffix}", method = RequestMethod.GET)
    public String index(@PathVariable("siteId") String siteId, Model model) {
        // 获取网站信息
        Site site = siteService.get(siteId);
        model.addAttribute("site", site);
        return "cms/theme/" + site.getTheme() + "/index";
    }

    /**
     * 用户个人中心
     * @return
     */
    @RequestMapping(value = "/{siteId}/personal${urlSuffix}", method = RequestMethod.GET)
    public String personal(@PathVariable("siteId") String siteId,Model model){
        // 获取网站信息
        Site site = siteService.get(siteId);
        model.addAttribute("site", site);
        return "cms/theme/" + site.getTheme() + "/personal";
    }

    /**
     * 访问导航
     *
     * @param siteId 站点ID
     * @param navId  导航ID
     * @param model
     * @return
     */
    @RequestMapping(value = "/{siteId}/{navId}/nav${urlSuffix}", method = RequestMethod.GET)
    public String nav(@PathVariable("siteId") String siteId,
                      @PathVariable("navId") String navId, @RequestParam(required = false) String categoryId, Model model) {
        // 获取网站信息
        Site site = common(siteId, navId, categoryId, model);
        if (JStringUtils.isNotBlank(categoryId)) {
            model.addAttribute("categorys", Lists.newArrayList(categoryService.get(categoryId)));
        }
        return "cms/theme/" + site.getTheme() + "/nav";
    }

    /**
     * 访问自定义视图
     *
     * @return
     */
    @RequestMapping(value = "/{siteId}/{navId}/view/{view}${urlSuffix}", method = RequestMethod.GET)
    public String customView(@PathVariable("siteId") String siteId,
                             @PathVariable("navId") String navId, @PathVariable("view") String view, @RequestParam(required = false) String categoryId, Model model) {
        // 获取网站信息
        Site site = common(siteId, navId, categoryId, model);
        if (JStringUtils.isNotBlank(categoryId)) {
            model.addAttribute("categorys", Lists.newArrayList(categoryService.get(categoryId)));
        }
        return "cms/theme/" + site.getTheme() + "/" + view;
    }

    /**
     * 分享文章
     *
     * @param siteId
     * @param categoryId
     * @param model
     * @return
     */
    @RequestMapping(value = "/{siteId}/{navId}/{categoryId}/share${urlSuffix}", method = RequestMethod.GET)
    public String share(@PathVariable("siteId") String siteId, @PathVariable("navId") String navId, @PathVariable("categoryId") String categoryId, Model model) {
        // 获取网站信息
        Site site = common(siteId, navId, categoryId, model);
        return "cms/theme/" + site.getTheme() + "/share";
    }

    /**
     * 查询文章
     *
     * @param siteId
     * @param model
     * @return
     */
    @RequestMapping(value = "/{siteId}/{navId}/search${urlSuffix}", method = RequestMethod.GET)
    public String search(@PathVariable("siteId") String siteId, @PathVariable("navId") String navId,Page<Article> page,Model model) throws Exception {
        // 获取网站信息
        Site site = common(siteId, navId, null, model);
        String key = new String(page.getKey().getBytes("iso-8859-1"),"utf-8");
        page.setKey(key);
        model.addAttribute("page", page.setList(articleService.findPage(page)));
        return "cms/theme/" + site.getTheme() + "/search";
    }

    /**
     * 访问详情内容页面
     *
     * @param siteId    网站ID
     * @param articleId 内容ID
     * @return
     */
    @RequestMapping(value = "/{siteId}/{navId}/{articleId}/detail${urlSuffix}", method = RequestMethod.GET)
    public String detail(@PathVariable("siteId") String siteId, @PathVariable("navId") String navId, @PathVariable("categoryId") String categoryId,
                         @PathVariable("articleId") String articleId, Model model) {
        // 获取网站信息
        Site site = common(siteId, navId, categoryId, model);
        // 获取详细内容
        Article article = articleService.get(articleId);
        model.addAttribute("article", article);
        ArticleData articleData = articleDataService.get(articleId);
        model.addAttribute("articleData", articleData);
        // 访问＋1
        articleService.updateHitsAddOne(articleId);
        // 获取网站主题
        return "cms/theme/" + site.getTheme() + "/detail";
    }

    /**
     * 公共方法
     *
     * @param siteId
     * @param navId
     * @param categoryId
     */
    public Site common(String siteId, String navId, String categoryId, Model model) {
        Site site = siteService.get(siteId);
        model.addAttribute("site", site);
        model.addAttribute("category", categoryService.get(navId));
        Category category = new Category();
        category.setParentId(navId);
        model.addAttribute("categoryNexts", categoryService.findList(category));
        model.addAttribute("categoryId", categoryId);
        return site;
    }
}
